package com.untilov.gb.me.gui;

import javax.microedition.io.ConnectionNotFoundException;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;

import com.untilov.gb.me.GoogleBookmarksMIDlet;
import com.untilov.gb.model.FeedItem;

/**
 * Class represent details of selected bookmark(feed),
 * and allows to navigate to this bookmark, or just copy it.
 * 
 * @author iuntilov
 *
 */
public class FeedDetailsForm extends Form implements CommandListener {
    
    final int URL_TEXT_SIZE = 512;
    
    private GoogleBookmarksMIDlet midlet;
    
    private Alert platformRequestFailed;
    
    private FeedItem currentFeed;
    
    private StringItem category;
    
    private StringItem title;
    
    private TextField url;
    
    private Command back;
    
    private Command show;
    
//    private Command copy;

    public FeedDetailsForm(String title, GoogleBookmarksMIDlet midlet) {
        super(title);
        this.midlet = midlet;
        category = new StringItem("Category", null);
        append(category);
        this.title = new StringItem("Title", null);
        append(this.title);
        url = new TextField("URL", null, URL_TEXT_SIZE, TextField.ANY);
        append(url);
        setCommandListener(this);
        back = new Command("Back", Command.BACK, 1);
        addCommand(back);
        show = new Command("Open URL", Command.SCREEN, 1);
        addCommand(show);
//        copy = new Command("Copy URL", Command.SCREEN, 1);
//        addCommand(copy);
    }

    public void commandAction(Command c, Displayable d) {
        if (c == back) {
            midlet.showCategoryContent(currentFeed.getCategory());
        } else if (c == show) {
            try {
                boolean needExitBeforeBrowserCanStart = midlet.
                    platformRequest(url.getString());
                if (needExitBeforeBrowserCanStart) {
                    midlet.destroyApp(true);
                }
            } catch (ConnectionNotFoundException e) {                
                e.printStackTrace();
                midlet.getDisplay().setCurrent(getPlatformRequestFailedAlert());
            }
        } 
//        else if (c == copy) {
//            // TODO: invistigate and implement if possible
//        }
    }
    
    public FeedDetailsForm showBookmark(FeedItem item) {
        currentFeed = item;
        category.setText(currentFeed.getCategory());
        title.setText(currentFeed.getTitle());
        url.setString(currentFeed.getUrl());
        return this;
    }
    
    private Alert getPlatformRequestFailedAlert() {
        if (platformRequestFailed == null) {
            platformRequestFailed = new Alert("Exception", "Connection problems", 
                    null, AlertType.ERROR);
        }
        return platformRequestFailed;
    }
}
